package com.example.administrator.newproject;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;


public class WelcomeActivity extends ActionBarActivity {
    ViewPager pagerStart;
    private ArrayList<View> views;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        getSupportActionBar().hide();
        pagerStart = (ViewPager) findViewById(R.id.pager_welcome);


        ImageView view1 = new ImageView(this);
        view1.setImageResource(R.drawable.welcome1);
        view1.setScaleType(ImageView.ScaleType.FIT_XY);
        view1.setMaxWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        view1.setMaxHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        ImageView view2 = new ImageView(this);
        view2.setImageResource(R.drawable.welcome2);
        view2.setScaleType(ImageView.ScaleType.FIT_XY);
        view2.setMaxWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        view2.setMaxHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        ImageView view3 = new ImageView(this);
        view3.setImageResource(R.drawable.welcome3);
        view3.setScaleType(ImageView.ScaleType.FIT_XY);
        view3.setMaxWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        view3.setMaxHeight(LinearLayout.LayoutParams.MATCH_PARENT);


        views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        pagerStart.setAdapter(new StartAdapter());
         pagerStart.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
             @Override
             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

             }

             @Override
             public void onPageSelected(int position) {
                if(position==2){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Intent intent = new Intent();
                    intent.setClass(WelcomeActivity.this,MainActivity.class);
                    startActivity(intent);

                }
             }
             @Override
             public void onPageScrollStateChanged(int state) {

             }
         });

    }

    class StartAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }


}
